﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;

namespace Bing
{
    /// <summary>
    /// Contains a list of Assets for the Bing API
    /// </summary>
    [DebuggerDisplay("{BrandName} {SearchUrl} {ImageAssets.Count} image(s)")]
    public class BrandAssets
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BrandAssets"/> class.
        /// </summary>
        public BrandAssets() : this("http://api.bing.com/brandassets.xml") {}

        /// <summary>
        /// Initializes a new instance of the <see cref="BrandAssets"/> class.
        /// </summary>
        /// <param name="brandingUrl">The branding URL.</param>
        public BrandAssets(string brandingUrl)
        {
            BrandingUrl = brandingUrl;
            ImageAssets = new Dictionary<string, BrandImageAsset>();
        }

        #region Public Properties
        /// <summary>
        /// Gets or sets the search URL.
        /// </summary>
        /// <value>The search URL.</value>
        public string SearchUrl { get; set; }
        /// <summary>
        /// Gets or sets the search box URL.
        /// </summary>
        /// <value>The search box URL.</value>
        public string SearchBoxUrl { get; set; }
        /// <summary>
        /// Gets or sets the help page.
        /// </summary>
        /// <value>The help page.</value>
        public string HelpPage { get; set; }
        /// <summary>
        /// Gets or sets the name of the brand.
        /// </summary>
        /// <value>The name of the brand.</value>
        public string BrandName { get; set; }

        /// <summary>
        /// Gets or sets the branding URL.
        /// </summary>
        /// <value>The url that contains all of the Bing API Assets</value>
        public string BrandingUrl { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is loaded.
        /// </summary>
        /// <value><c>true</c> if this instance is loaded; otherwise, <c>false</c>.</value>
        public bool IsLoaded { get; set; }

        /// <summary>
        /// Gets or sets the image assets.
        /// </summary>
        /// <value>The List of <see cref="BrandImageAsset"/>s.</value>
        public Dictionary<string, BrandImageAsset> ImageAssets { get; set; }

        #endregion Public Properties

        #region Public Methods
        /// <summary>
        /// Loads this instance.
        /// </summary>
        public void Load()
        {
            Load(BrandingUrl);   
        }

        /// <summary>
        /// Loads the specified branding URL.
        /// </summary>
        /// <param name="brandingUrl">The branding URL.</param>
        public void Load(string brandingUrl)
        {
            try
            {
                BrandingUrl = brandingUrl;
                var branding = XElement.Load(BrandingUrl);
                SearchUrl = ApiHelper.GetElementValueOrDefault<string>(branding, "SearchUrl");
                SearchBoxUrl = ApiHelper.GetElementValueOrDefault<string>(branding, "SearchBoxUrl");
                HelpPage = ApiHelper.GetElementValueOrDefault<string>(branding, "HelpPage");
                BrandName = ApiHelper.GetElementValueOrDefault<string>(branding, "BrandName");

                ImageAssets = new Dictionary<string, BrandImageAsset>();
                XElement imageResults = branding.Element("Images");
                XElement scriptResults = branding.Element("Scripts");

                if (imageResults != null)
                {
                    foreach (XElement image in imageResults.Elements())
                    {
                        BrandImageAsset imageAsset = new BrandImageAsset();
                        imageAsset.Name = image.Name.ToString();
                        // The URL is in the value of the image variable.
                        // Pull it out with a regular expression
                        imageAsset.Url = ApiHelper.GetUrl(image.Value);
                        imageAsset.Width = ApiHelper.GetElementValueOrDefault<UInt32>(image, "Width");
                        imageAsset.Height = ApiHelper.GetElementValueOrDefault<UInt32>(image, "Height");

                        ImageAssets.Add(imageAsset.Name, imageAsset);
                    }

                }

                if (scriptResults != null)
                {
                    // TODO: Add when we have items in the Scripts element.
                }


            }
            catch (Exception)
            {
                IsLoaded = false;
                throw;
            }

            IsLoaded = true;
        }
        #endregion Public Methods

       

    }
}
